package jp.ac.u_tokyo.dch.dwr;

import java.util.HashMap;
import java.util.List;

import jp.ac.u_tokyo.dch.core.db.DBConnection;
import jp.ac.u_tokyo.dch.core.exceptions.DBException;
import jp.ac.u_tokyo.dch.dao.data.DataDao;
import jp.ac.u_tokyo.dch.dao.psi.PsiInterfaceDao;
import jp.ac.u_tokyo.dch.dao.schema.DataSchemaDao;
import jp.ac.u_tokyo.dch.model.db.DataSchema;
import jp.ac.u_tokyo.dch.util.DchLogger;
import jp.ac.u_tokyo.dch.util.common.CommonDBConnection;

/**
 *
* @version 1.1
 *
 */
public class DchDwr {
	private DchLogger logger = DchLogger.getLogger(getClass());

	/**
	 * スキーマリストを取得する
	 * @param sort 分類
	 * @return スキーマリスト
	 */
	public List<DataSchema> getOutputSchemaList(String sort){

		DBConnection conn = null;
		List<DataSchema> list = null;

		try {
			 conn = CommonDBConnection.getDbconnection();
			 DataSchemaDao dataSchemaDao = new DataSchemaDao(conn);
			 list = dataSchemaDao.selectList(sort);
			 DataSchema schema = new DataSchema();
			 schema.setSchemaid("");
			 schema.setName("");
			 list.add(0, schema);
		} catch (Exception e) {
			logger.error(e);
		} finally {
			try {
				CommonDBConnection.closeConnection(conn);
			} catch (DBException dbe) {
			}
		}
		return list;

	}

	/**
	 * DWR用メソッド
	 * データ検索結果の件数を返す
	 * @param searchKeywordMap 検索キーワード
	 * @return 検索件数
	 * @throws Exception
	 */
	public int getSearchOutputDataResultCnt(HashMap<String,String> searchKeywordMap) {
		DBConnection conn = null;
		int resultCnt =0;
		try {
			 conn = CommonDBConnection.getDbconnection();
			 DataDao dataDao = new DataDao(conn);
			 resultCnt = dataDao.getSearchResultCnt(searchKeywordMap);
		} catch (Exception e) {
			logger.error(e);
		} finally {
			try {
				CommonDBConnection.closeConnection(conn);
			} catch (DBException dbe) {
			}
		}
		return resultCnt;
	}

	/**
	 * DWR用メソッド
	 * データ検索結果の件数を返す
	 * @param searchKeywordMap 検索キーワード
	 * @return 検索件数
	 * @throws Exception
	 */
	public int getSearchPsiDataResultCnt(HashMap<String,String> searchKeyMap) throws Exception {

		PsiInterfaceDao psiInterfaceDao = new PsiInterfaceDao();

		return psiInterfaceDao.getSearchResultCnt(searchKeyMap);
	}

	/**
	 * DWR用メソッド
	 * データ検索結果の件数を返す
	 * @param searchKeywordMap 検索キーワード
	 * @return 検索件数
	 * @throws Exception
	 */
	public int getSearchOyaDataResultCnt(HashMap<String,String> searchKeywordMap) {
		DBConnection conn = null;
		int resultCnt =0;
		try {
			 conn = CommonDBConnection.getDbconnection();
			 DataDao dataDao = new DataDao(conn);
			 resultCnt = dataDao.getOyaSearchResultCnt(searchKeywordMap);
		} catch (Exception e) {
			logger.error(e);
		} finally {
			try {
				CommonDBConnection.closeConnection(conn);
			} catch (DBException dbe) {
			}
		}
		return resultCnt;
	}



}
